#pragma once
#include"init.h"
#include <QtWidgets/QWidget>
#include "ui_front.h"
#include "game.h"

QT_BEGIN_NAMESPACE
namespace Ui { class frontClass; };
QT_END_NAMESPACE

class front : public QWidget
{
	Q_OBJECT

public:
	front(QWidget *parent = nullptr);
	~front();
private:
	Ui::frontClass *ui;
	bool readyButtonClicked = 0;
	game g;
private slots:
	void on_readyButton_clicked() {
		Mix_PlayChannel(-1, click, 0);
		if (!readyButtonClicked) {
			auto &bt = ui->widget;
			QPropertyAnimation *btAm = new QPropertyAnimation(bt, "pos");
			btAm->setEndValue(QPoint(bt->x() - 300, bt->y()));
			btAm->setDuration(1000);
			btAm->setEasingCurve(QEasingCurve::OutExpo);
			btAm->start();
			readyButtonClicked = 1;
		}
	}
	void on_helpButton_clicked() {
		Mix_PlayChannel(-1, click, 0);
		QMessageBox::information(this, "贪吃蛇帮助", "WASD移动\nQ减速\nE加速\n在蛇头某一朝向时按相应的方向键会突进");
	}
	void on_exitButton_clicked() {
		Mix_PlayChannel(-1, click, 0);
		this->close();
	}
	void on_startButton_clicked() {
		Mix_PlayChannel(-1, click, 0);
		Mix_HaltChannel(0);
		int num = ui->sizeEdit->text().toInt();
		g.resize(num * frame_size, num * frame_size);
		for (int i = 0;i < num;i++) {
			for (int j = 0;j < num;j++) {
				QLabel *frameLabel = new QLabel(&g);
				frameLabel->setGeometry(j * frame_size, i * frame_size, frame_size, frame_size);
				frameLabel->setPixmap(QPixmap::fromImage(frameImage));
				frameLabel->setScaledContents(1);
			}
		}
		g.init(ui->speedEdit->text().toInt(), num);
		this->close();
	}
};
